# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.paginator import Paginator
from myhomework.account.daos import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
from myhomework.exceptions.exception import *
from myhomework.account.decorators import *
from myhomework.common.decorators import *
from myhomework.common.daos import *
from myhomework.common.forms import *
from django.template import RequestContext
from myhomework.comment.daos import *
from myhomework.comment.forms import *


class CommentView(object):
    
    """
    def create_homework(request,context={}):
        if request.method == 'POST':          
            form = CreateHomeworkForm(request.POST)
            if form.is_valid():
                homework = form.save(commit=False)
                HomeworkDao.add_homework(homework)
                print 'id!!!!!!!!!!!'
                print homework.id
                return HttpResponse('{"homework":"%d"}'%(homework.id,))
            else:
                raise UserDataFormError(form.errors)
        
        return render_to_response('ajax/create_homework.html',context, context_instance=RequestContext(request))
    """
    
    @staticmethod
    #@login_required
    #@process_exception
    def create_comment(request,context={}):
        
        if request.method == 'POST':          
            form = CreateCommentForm(request.POST)
            
            if form.is_valid():
                comment = form.save(commit=False)
                
                print request.user.username
                
                parent_comment_id = request.POST.get('parent_comment')
                
                try:
                    parent_comment = CommentDao.get_comment_by_id(parent_comment_id)
                except Exception,e:
                    parent_comment = None
                print parent_comment_id
                print 'comment'
                print parent_comment
                comment.parent_comment = parent_comment
                CommentDao.create_comment(request.user.username,comment)
                
                context.update({'comments':[comment]})
                
                return render_to_response('ajax/comment_json.html',context, context_instance=RequestContext(request))
            
            else:
                raise UserDataFormError(form.errors)
        
        return render_to_response('ajax/create_create.html',context, context_instance=RequestContext(request))
           
    @staticmethod
    def delete_comment(request,context={}):
        
        comment_id = request.GET.get('comment')
        CommentDao.delete_comment(comment_id)
        
        return HttpResponse('{}')
    
    @staticmethod    
    def get_comments(request,context={}):
        homework_id= request.GET.get('homework')
        comments = CommentDao.get_comments(homework_id)

        
        context.update({'comments':comments})
        
        return render_to_response('ajax/comment_json.html',context, context_instance=RequestContext(request))
        
  